$(document).ready(function ($) {
  //*fixed content height
  var browserHeight = $(window).height()
  var headerHeight = $('.modules-navigation-bar').height()
  var footerHeight =
    $('.modules-sitemap').height() + $('.modules-footer').height()
  var articleHeight = browserHeight - headerHeight - footerHeight - 100 //100 = article margin-bottom
  $('.main-article').attr('style', 'min-height:' + articleHeight + 'px;')

  openVertMenu()

  //footer - newsletter
  $('#signup-newsletter').focusin(function () {
    $('.signup-input-part').addClass('signup-hover')
    $('.signup-input').addClass('shorten-input')
    $('.signup-btn').show()
  })
  $(window).click(function () {
    $('.signup-input-part').removeClass('signup-hover')
    $('.signup-input').removeClass('shorten-input')
    $('.signup-btn').hide()
    $('#signup-newsletter').val('')
  })
  $('.signup-input-part').click(function (event) {
    event.stopPropagation()
  })

  //Subcribe newsletter
  //press enter
  $('#signup-newsletter').keypress(function (e) {
    var key = e.which
    if (key == 13) {
      // the enter key code

      $('.signup-btn').click()
      return false
    }
  })

  $('.signup-btn').click(function () {
    var email = $('#signup-newsletter').val()

    if (!validateEmail(email)) {
      submit = false
      $('.newsletter-warning').show()
    } else {
      submit = true
      $('.newsletter-warning').hide()
    }
    if (submit) {
      $.ajax({
        type: 'POST',
        url: '/index.php?option=com_ajax&module=navicat_ajax&format=raw&method=newsletterSubscribe',
        dataType: 'json',
        data: {
          newsletterVal: email
        }
      }).done(function (data) {
        if (data.result == 1) {
          location.href = '/company/newsletter'
          return
        }
        if (data.result == 2) {
          location.href = '/company/newsletter-already-subscribed'
          return
        }
        /*if ( data.result == 3 ){

					return;
				}*/
      })
    }
  })

  //MySQL + Postgred, app icon hover effect
  $('#mysqlIosIcon').mouseover(function () {
    $(this).attr('src', '/images/02.Product_01_MySQL_AppIcon_hover.png')
  })
  $('#mysqlIosIcon').mouseout(function () {
    $(this).attr('src', '/images/02.Product_01_MySQL_AppIcon.png')
  })
  $('#postIosIcon').mouseover(function () {
    $(this).attr('src', '/images/02.Product_01_PostgreSQL_AppIcon_hover.png')
  })
  $('#postIosIcon').mouseout(function () {
    $(this).attr('src', '/images/02.Product_01_PostgreSQL_AppIcon.png')
  })
  $('#mariadbIosIcon').mouseover(function () {
    $(this).attr('src', '/images/02.Product_01_MariaDB_AppIcon_hover.png')
  })
  $('#mariadbIosIcon').mouseout(function () {
    $(this).attr('src', '/images/02.Product_01_MariaDB_AppIcon.png')
  })

  $(window).resize(function () {
    var windoWwidth = $(window).width()
    /*if (windoWwidth > 972) {
			closeVertMenu();
			$(".close-menu-btn a img").click();
			$(".nav-container").removeClass("mobile-nav-container");
		}*/
    /*if (windoWwidth >= 768) {
			$(".sitemap-items").addClass("show-items");
			$(".sitemap-btn>img").attr("status","off");
			$(".sitemap-btn>img").attr("src","/images/00_expand_down_icon.svg");
		}else{
			$(".sitemap-items").removeClass("show-items");
		}*/
  })

  jQuery('.prod-buy-quanity').val('1')

  $('.sitemap-btn').click(function () {
    var this_ = $(this).find('img')
    var this_status = this_.attr('status')

    if (this_status == 'off') {
      this_.attr('status', 'on')
      this_.attr('src', './images/expand_up_icon.svg')
      this_
        .parent()
        .parent()
        .parent()
        .parent()
        .find('.sitemap-items')
        .addClass('show-items')
    } else {
      console.log('to off')
      this_.attr('status', 'off')
      this_.attr('src', './images/expand_down_icon.svg')
      this_
        .parent()
        .parent()
        .parent()
        .parent()
        .find('.sitemap-items')
        .removeClass('show-items')
    }
  })

  $('.search-icon img').click(function () {
    $('.modules-smart-search').toggle()
  })

  $('.search-btn').click(function () {
    $('.form-search').submit()
  })

  $('.checkbox-label').click(function () {
    $(this).parent().find("input[type='checkbox']").click()
  })

  $('.show-hide-control > span').click(function () {
    $(this).parent().toggleClass('show')
    $(this).parent().prev('.show-hide-content').toggleClass('show')
  })

  $('.contact_country').click(function () {
    $('.contact-address2-row, .contact-fax-row, .g-map').hide()
    $('.contact_country').removeClass('selected')
    var place = $(this).attr('ref')
    if (!$(this).hasClass('selected')) {
      switch (place) {
        case 'hk':
          $(".contact_country[ref='hk']").addClass('selected')
          $('#map_hk').show()
          $('.contact-address').html(
            'Unit 1605-1607, Level 16, Tower 1, Grand Century Place, Mongkok, Kowloon, Hong Kong SAR'
          )
          $('.contact-num').html('+852 - 2498 3422')
          $('.contact-hour').html('9:30 - 18:30 (GMT + 8)')
          break
        case 'cn':
          $(".contact_country[ref='cn']").addClass('selected')
          $('#map_cn').show()
          $('.contact-address').html(
            'Unit 607, Tower B, Venture International Park, 2679 Hechuan Road, Shanghai, China (TBC)'
          )
          $('.contact-num').html('+86 21 3461 7361')
          $('.contact-hour').html('9:30 - 18:30 (GMT + 8)')
          break
        case 'jp':
          $(".contact_country[ref='jp']").addClass('selected')
          $('#map_jp').show()
          $('.contact-address').html(
            'Shinjuku i-Land Tower. 22F, 6-5- 1, Nishishinjuku, Shinjuku-ku, Tokyo, 163-1326, Japan'
          )
          $('.contact-num').html('03-5354- 5751 (or +81-3- 5354-5751)')
          $('.contact-hour').html('9:30 - 18:30 (GMT + 9)')
          $('.contact-address2-row, .contact-fax-row').show()
          break
      }
    }
  })

  //info-switcher-in-3**************************************************************
  $('.info-switcher-in3, .info-switcher-in4').click(function () {
    $('.info-switcher-in3, .info-switcher-in4').removeClass('selected')
    $(this).addClass('selected')
    $('.info-content').hide()
    $('.product-info-features').show()
    var os = $(this).attr('os')
    switch (os) {
      case 'win':
        $('.Windows-container').show()
        $('.system-requirements-table').show()
        break
      case 'mac':
        $('.Mac-container').show()
        $('.system-requirements-table').show()
        break
      case 'linux':
        $('.Linux-container').show()
        $('.system-requirements-table').show()
        break
      case 'ios':
        $('.IOS-container').show()
        $('.product-info-features').hide()
        break
    }
  })
  //info-switcher-in-3**************************************************************

  var navigation_bar_height = $('.modules-navigation-bar').height()
  var footer_height =
    $('.modules-sitemap').height() + $('.modules-footer').height()
  var window_height = $(window).height()

  $(window).scroll(function () {
    scroll_top = $(window).scrollTop()
    if (scroll_top >= navigation_bar_height) {
      $('.sub-navigation-bar-background').addClass('fixed')
      $('.sub-navigation-bar-placeholder').show()
    } else {
      $('.sub-navigation-bar-background').removeClass('fixed')
      $('.sub-navigation-bar-placeholder').hide()
    }
    if (scroll_top >= 50) {
      $('.fix-right-bottom-btn').fadeIn()
    } else {
      $('.fix-right-bottom-btn').fadeOut()
    }
  })

  if (sessionStorage.getItem('numberOfItem') > 0) {
    $('.number-of-item').html(sessionStorage.getItem('numberOfItem')).show()
  } else {
    $('.number-of-item').remove()
  }

  /*Press*/
  var press_pagination = $('.blog .pagination')
  press_pagination.wrap("<div class='container pagination-container'></div>")
  /*press_pagination.find(".pagination-start").html("<span><<</span>");
	press_pagination.find(".pagination-prev").html("<span><</span>");
	press_pagination.find(".pagination-next").html("<span>></span>");
	press_pagination.find(".pagination-end").html("<span>>></span>");*/
  $('.blog .show-hide-content').first().addClass('show')
  $('.blog .show-hide-control').first().addClass('show')

  back_to_top()
  function back_to_top () {
    $('.back-to-top-btn').click(function () {
      $('html, body').animate({ scrollTop: 0 }, 'slow')
      return false
    })
  }

  /*function closeVertMenu(){
		$(".close-menu-btn a img").click(function(){

			$(".nav-container").removeClass("mobile-nav-container");
			$(".vertical-menu-container").slideUp();
			$(".search-icon, .cart-icon, .language-icon").show();
			setTimeout(function(){
				$(".nav-container").removeClass("dark-bg");
				$(".navicat-logo>a>img").addClass("m-logo");

				windoWwidth = $(window).width();
				if(windoWwidth < 370) {
					$(".navicat-logo>a>img").addClass("m-logo");
					$(".navicat-logo>a>img").attr("src","/images/00_navicat_logo_small_rainbow.png");
				}else{
					$(".navicat-logo>a>img").removeClass("m-logo");
					$(".navicat-logo>a>img").attr("src","/images/00_navicat_logo_rainbow.svg");
				}

			}, 500);
		});
	}*/

  function openVertMenu () {
    $('.menu-icon a img').click(function () {
      $('.navicat-logo>a>img').removeClass('m-logo')
      $('.navicat-logo>a>img').attr('src', '/images/00_navicat_logo_white.svg')
      $('.nav-container').addClass('mobile-nav-container')
      $('.nav-container').addClass('dark-bg')
      $('.search-icon, .cart-icon, .language-icon').hide()

      setTimeout(function () {
        $('.vertical-menu-container').slideDown()
      }, 200)
    })
  }

  $('.sub-navigation-expand-button').click(function () {
    if ($(this).attr('status') == 'closed') {
      $(this).attr('status', 'open')
      $(this).find('img').attr('src', '../../images/00_SubNaviBar_hide.svg')
      $('.sub-navigation-bar-background').addClass('expand')
    } else {
      $(this).attr('status', 'closed')
      $(this).find('img').attr('src', '../../images/00_SubNaviBar_expand.svg')
      $('.sub-navigation-bar-background').removeClass('expand')
    }
  })

  // $('#freeTrailBtn').click(function () {
  //   $.ajax({
  //     type: 'POST',
  //     url: '/includes/Navicat/industry.php',
  //     dataType: 'json',
  //     data: {
  //       action: 'getCompany',
  //       industry: $('#industry-select').val(),
  //       country: $('#country-select').val()
  //     }
  //   }).done(function (data) {
  //     $('.name-list').empty()
  //     if (data == 'none') {
  //       $('.name-list').append('<div>找不到记录！<div>')
  //     } else {
  //       $.each(data.result, function (i, item) {
  //         var get_industry =
  //           "<div class='col-md-4 col-sm-4 col-xs-6'>" + item.company + '</div>'
  //         $('.name-list').append(get_industry)
  //       })
  //     }
  //   })
  // })

  /******* Search ******/
  if ($('#q').val() == '') {
    $('.page-container').append('<h2>请在搜索框中输入你的搜索字词。</h2>')
  }

  jQuery('.faq-btn').click(function () {
    if (jQuery(this).attr('status') == 'on') {
      jQuery(this).attr('status', 'off')
      jQuery(this)
        .find('img')
        .attr('src', '../../images/00_expand_down_icon.svg')
      jQuery(this).parent().parent().find('.faq-content').hide()
    } else {
      jQuery(this).attr('status', 'on')
      jQuery(this).find('img').attr('src', '../../images/00_hide_up_icon.svg')
      jQuery(this).parent().parent().find('.faq-content').show()
    }
  })

  //wechat QR code
  jQuery('.wechat-small-qr-code-btn').hover(
    function () {
      $('.wechat-small-qr-code').addClass('hover')
    },
    function () {
      $('.wechat-small-qr-code').removeClass('hover')
    }
  )

  //footer - show hide wechat QRcode
  jQuery('.footer-wechat-btn').click(function () {
    jQuery('.footer-wechat-qrcode-container').show()
  })
  jQuery('.footer-wechat-qrcode-cancel-btn').click(function () {
    jQuery('.footer-wechat-qrcode-container').hide()
  })
})
